package com.example.iotmanagerment26.controller;

import com.example.iotmanagerment26.domain.Device;
import com.example.iotmanagerment26.domain.JsonData;
import com.example.iotmanagerment26.domain.Room;
import com.example.iotmanagerment26.service.KeyCheckService;
import com.example.iotmanagerment26.service.RoomService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/v1/room")
@Tag(name="房间管理",description = "房间管理的接口")
public class RoomController {

    @Autowired
    RoomService roomService;
    @Autowired
    KeyCheckService keyCheckService;

    @GetMapping("/selectAllRoom")
    @Operation(summary = "查询所有房间信息")
    public JsonData selectAllRoom() {
        List<Room> result = roomService.selectAllRoom();
        if (result!=null) {
            return new JsonData(1,result,"查询成功");
        }else {
            return new JsonData(0,result,"查询失败");
        }
    }

    @GetMapping("/selectRoomInfoByName")
    @Operation(summary = "通过房间名查询房间设备和用户信息")
    public JsonData selectRoomInfoByName(String roomName) {
        List<Room> result = roomService.selectRoomInfoByName(roomName);
        if (result!=null) {
            return new JsonData(1,result,"查询成功");
        }else {
            return new JsonData(0,result,"查询失败");
        }
    }

    @GetMapping("/selectRoomAndDeviceByUser")
    @Operation(summary = "通过用户名查询房间和设备")
    public JsonData selectRoomAndDeviceByUser(String userName) {
        List<Map<String,Object>> result = roomService.selectRoomAndDeviceByUser(userName);
        if (result!=null) {
            return new JsonData(1,result,"查询成功");
        }else {
            return new JsonData(0,result,"查询失败");
        }
    }

    @GetMapping("/selectAllUnbindRoomUser")
    @Operation(summary = "查询所有未绑定房间的用户")
    public JsonData selectAllUnbindRoomUser() {
        List<Map<String,Object>> result = roomService.selectAllUnbindRoomUser();
        if (result!=null) {
            return new JsonData(1,result,"查询成功");
        }else {
            return new JsonData(0,result,"查询失败");
        }
    }

    @GetMapping("/selectAllUnbindRoomDevice")
    @Operation(summary = "查询所有未绑定房间的设备")
    public JsonData selectAllUnbindRoomDevice() {
        List<Map<String,Object>> result = roomService.selectAllUnbindRoomDevice();
        if (result!=null) {
            return new JsonData(1,result,"查询成功");
        }else {
            return new JsonData(0,result,"查询失败");
        }
    }

    @PostMapping("/insertRoom")
    @Operation(summary = "增加新房间")
    public JsonData insertRoom(String name,String key){
        if (!keyCheckService.check(key)) {
            return keyCheckService.sendKeyError();
        }
        int result=roomService.insertRoom(name);
        if (result>=1){
            return new JsonData(1,name,"添加成功");
        }else if(result==-1){
            return new JsonData(0,result,"添加失败,已经有相同的房间了！");
        }else{
            return new JsonData(0,result,"添加失败");
        }
    }

    @PostMapping("/insertRoomDevice")
    @Operation(summary = "给房间名增加新的设备")
    public JsonData insertRoomDevice(String name,String device,String key){
        if (!keyCheckService.check(key)) {
            return keyCheckService.sendKeyError();
        }
        int result=roomService.insertRoomDevice(name,device);
        if (result>=1){
            return new JsonData(1,name,"房间:"+name+" 添加设备:"+device+"成功！");
        }else if(result==-1){
            return new JsonData(0,result,"添加失败,该房间已经有相同的设备了！");
        }else if(result==-2){
            return new JsonData(0,result,"添加失败,该房间不存在！");
        }else{
            return new JsonData(0,result,"添加失败");
        }
    }

    @PostMapping("/insertRoomUser")
    @Operation(summary = "给房间名增加新的用户")
    public JsonData insertRoomUser(String name,String user,String key){
        if (!keyCheckService.check(key)) {
            return keyCheckService.sendKeyError();
        }
        int result=roomService.insertRoomUser(name,user);
        if (result>=1){
            return new JsonData(1,name,"房间:"+name+" 添加用户:"+user+"成功！");
        }else if(result==-1){
            return new JsonData(0,result,"添加失败,该房间已经有相同的用户了！");
        }else if(result==-2){
            return new JsonData(0,result,"添加失败,该房间不存在！");
        }else{
            return new JsonData(0,result,"添加失败");
        }
    }

    @DeleteMapping("/deleteRoom")
    @Operation(summary = "通过房间名字删除房间")
    public JsonData deleteRoomByName(String name,String key){
        if (!keyCheckService.check(key)) {
            return keyCheckService.sendKeyError();
        }
        int result= roomService.deleteRoom(name);
        if (result>=1){
            return new JsonData(1,name,"删除成功");
        }else {
            return new JsonData(0,name,"删除失败");
        }
    }

    @DeleteMapping("/deleteRoomUser")
    @Operation(summary = "通过房间名字和用户名删除房间的用户")
    public JsonData deleteRoomUser(String name,String user,String key){
        if (!keyCheckService.check(key)) {
            return keyCheckService.sendKeyError();
        }
        int result= roomService.deleteRoomUser(name,user);
        if (result>=1){
            return new JsonData(1,name,"删除成功");
        }else {
            return new JsonData(0,name,"删除失败");
        }
    }

    @DeleteMapping("/deleteRoomDevice")
    @Operation(summary = "通过房间名字和设备名删除房间的设备")
    public JsonData deleteRoomDevice(String name,String device,String key){
        if (!keyCheckService.check(key)) {
            return keyCheckService.sendKeyError();
        }
        int result= roomService.deleteRoomDevice(name,device);
        if (result>=1){
            return new JsonData(1,name,"删除成功");
        }else {
            return new JsonData(0,name,"删除失败");
        }
    }
}
